package co.edu.udea.convid;

import java.io.File;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FragmentoEnvioDatos extends Fragment implements OnClickListener, LocationListener
{
	//componentes de programacion
	private static final String TAG = "EnvioDatos";
	Uri URI = null;
	private static final int TOMAR_FOTO = 101;
	private LocationManager gestorUbicacion;
	private String provider;
	
	//componentes fisicos
	private Button btnAdjuntar;
	private ImageView imagen;
	private TextView ubicacionDispositivo;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{		
		View view = inflater.inflate(R.layout.fragmento_envio_datos, container, false);
		
		btnAdjuntar = (Button) view.findViewById(R.id.fed_boton_adjuntar_imagen);
		btnAdjuntar.setOnClickListener(this);
		ubicacionDispositivo = (TextView) view.findViewById(R.id.fed_campotexto_ubicacion);
		
		LocationManager service = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
		boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
		if (!enabled)
		{
			Log.d(TAG, "Habilitando GPS");
			Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
			startActivity(intent);			
		}
		
		gestorUbicacion = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
		
		Criteria criteria = new Criteria();
		provider = gestorUbicacion.getBestProvider(criteria, false);
		Location ubicacion = gestorUbicacion.getLastKnownLocation(provider);
		Log.d(TAG, "Provedor " + provider);
		
		if (ubicacion != null)
		{
			 System.out.println("Provedor " + provider + ".");
			 int lat = (int) (ubicacion.getLatitude());
			 int lng = (int) (ubicacion.getLongitude());
			 ubicacionDispositivo.setText(String.valueOf(lat) + " - " + String.valueOf(lng));
			 onLocationChanged(ubicacion);			 
		}
		else
		{
			ubicacionDispositivo.setText("Ubicacion no disponible");			 
		}
		
		return view;
	}

	@Override
	public void onClick(View v)
	{
		if(v == btnAdjuntar)
		{
			Log.d(TAG, "Adjuntando imagen");			
			openGallery();
		}	
	}
	
	public void openGallery()
	{
		 /*para adjuntar imagen*/
         /*Intent intent = new Intent();
         intent.setType("image/*");
         intent.setAction(Intent.ACTION_GET_CONTENT);
         intent.putExtra("return-data", true);*/
		 Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
		 File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
		 Log.d(TAG, "Ambiente "+dir.toString());
		 File output = new File(dir,"tmp.png");
		 intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(output));
		 String url = output.toURI().toString();
         Log.d(TAG, "URL de Archivo: "+url);
         URI = Uri.parse(output.toURI().toString());
		 startActivityForResult(intent, TOMAR_FOTO);
         //cambiarImagen(output.toURI().toString());
	 }
	
	private void cambiarImagen(String urlimagen)
	{
		URI = Uri.parse(urlimagen);
		imagen.setImageURI(URI);
	}

	@Override
	public void onLocationChanged(Location location)
	{
		 int lat = (int) (location.getLatitude());
		 int lng = (int) (location.getLongitude());
		 ubicacionDispositivo.setText(String.valueOf(lat) + " - " + String.valueOf(lng));
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider)
	{	
		Toast.makeText(this.getActivity(), "Enabled new provider " + provider, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}
}
